package com.dmgameclient.common;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

/**
 * @name ConnectivityUtils网络连接工具类判断当前网络连接状况
 * @Descripation <br>
 *               1、判断当前网络状况。<br>
 *               2、类型：Wifi、Mobile、Wifi或Mobile。<br>
 * @author 樊俊彬
 * @date 2014-9-17
 * @version 1.0
 */
public class ConnectivityUtils {

	private static final String TAG = "ConnectivityUtils";
	private static ConnectivityManager connectivityManager;

	/**
	 * 判断当前网络连接状况
	 * 
	 * @param context
	 * @return boolean
	 */
	public boolean isNetworkConnected(Context context) {
		connectivityManager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo mobileInfo = connectivityManager
				.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
		NetworkInfo wifiInfo = connectivityManager
				.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

		if ((mobileInfo != null && mobileInfo.isConnected())
				|| (wifiInfo != null && wifiInfo.isConnected())) {
			Log.i(TAG, ">>> 当前网络良好");
			return true;
		} else {
			Log.i(TAG, ">>> 当前网络不给力");
			return false;
		}
	}

	/**
	 * 判断手机GPRS网络连接状况
	 * 
	 * @param context
	 * @return boolean
	 */
	public boolean isMoblileConnected(Context context) {
		connectivityManager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo mobileInfo = connectivityManager
				.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
		if (mobileInfo != null && mobileInfo.isConnected()) {
			Log.i(TAG, ">>>手机GPRS网络连接良好");
			return true;
		} else {
			Log.i(TAG, ">>>手机GPRS网络无连接");
			return false;
		}
	}

	/**
	 * 判断Wifi网络连接状况
	 * 
	 * @param context
	 * @return boolean
	 */
	public boolean isWiFiConnected(Context context) {
		connectivityManager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo wifiInfo = connectivityManager
				.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
		if (wifiInfo != null && wifiInfo.isConnected()) {
			Log.i(TAG, ">>>WiFi网络连接良好");
			return true;
		} else {
			Log.i(TAG, ">>>WiFi网络无连接");
			return false;
		}
	}
}
